import os


############需要修改###################
# 要删除图像的长宽
H = 2592 
W = 640
# 指定文件夹位置
images_folder = "/home/hw/dataset/cone_total_new/images"
labels_folder = "/home/hw/dataset/cone_total_new/labels"
#####################################

def delete_images_and_labels(images_folder, labels_folder):
    # 获取images文件夹下尺寸为2592x640的图片列表
    image_files = [f for f in os.listdir(images_folder) if os.path.isfile(os.path.join(images_folder, f))]
    images_to_delete = [f for f in image_files if os.path.splitext(f)[1].lower() in ('.jpg', '.jpeg', '.png') and is_target_size(os.path.join(images_folder, f))]

    # 遍历删除符合条件的图片和对应编号的txt文件
    for image_file in images_to_delete:
        image_name, _ = os.path.splitext(image_file)
        txt_file = image_name + ".txt"
        
        image_path = os.path.join(images_folder, image_file)
        txt_path = os.path.join(labels_folder, txt_file)

        if os.path.exists(image_path):
            os.remove(image_path)
            print(f"Deleted image: {image_file}")

        if os.path.exists(txt_path):
            os.remove(txt_path)
            print(f"Deleted label: {txt_file}")

def is_target_size(image_path):
    try:
        from PIL import Image
        img = Image.open(image_path)
        width, height = img.size
        return width == H and height == W
    except Exception as e:
        print(f"Error processing {image_path}: {e}")
        return False

if __name__ == "__main__":

    delete_images_and_labels(images_folder, labels_folder)